<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Group;
use App\Models\GroupPost;
use App\User;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class GroupPostController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('内容');
            $content->description('管理');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('内容');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('添加');
            $content->description('内容');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(GroupPost::class, function (Grid $grid) {


            $grid->id('ID')->sortable();

            $grid->user()->name("发布用户");
            $grid->group()->title("圈子");
            $grid->cover('封面')->image('', '',50);
            $grid->column('title','标题');
            $grid->column('view_count','查看量')->sortable();
            $grid->column('reply_count','回复量')->sortable();
            $grid->column('last_reply_time','上次回复时间')->sortable();
            $grid->column('status','状态')->switch();
            $grid->column('is_top','是否置顶')->switch();


            $grid->updated_at('操作时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(GroupPost::class, function (Form $form) {

            $form->select('user_id', '用户')->options(User::all()->pluck('name', 'id'));
            $form->select('group_id', '圈子')->options(Group::all()->pluck('title', 'id'));
            $form->text('title','标题');
            $form->editor('content','内容');
            $form->switch('status','状态');
            $form->switch('is_top','是否置顶');
            $form->image('cover','封面');
            $form->multipleImage('images','图片组');


        });
    }
}
